#!/usr/bin/env pwsh

# 保存原始工作目录
$originalDir = Get-Location

# Start FastAPI Service Script

# Check and start required dependencies (MySQL and Redis)
function Check-And-Start-Dependencies {
    Write-Host "=========================================" -ForegroundColor Cyan
    Write-Host "Checking required dependencies..." -ForegroundColor Cyan
    Write-Host "=========================================" -ForegroundColor Cyan
    
    # Check and start MySQL if not running
    $mysqlRunning = docker ps | Select-String -Pattern "mysql-server"
    if (-not $mysqlRunning) {
        Write-Host "MySQL service not running. Starting MySQL..." -ForegroundColor Yellow
        try {
            . "$PSScriptRoot\01start_mysql.ps1"
            Write-Host "MySQL started successfully." -ForegroundColor Green
        } catch {
            Write-Host "Failed to start MySQL: $_" -ForegroundColor Red
            exit 1
        }
    } else {
        Write-Host "MySQL service is already running." -ForegroundColor Green
    }
    
    # Check and start Redis if not running
    $redisRunning = docker ps | Select-String -Pattern "redis-server"
    if (-not $redisRunning) {
        Write-Host "Redis service not running. Starting Redis..." -ForegroundColor Yellow
        try {
            . "$PSScriptRoot\02start_redis.ps1"
            Write-Host "Redis started successfully." -ForegroundColor Green
        } catch {
            Write-Host "Failed to start Redis: $_" -ForegroundColor Red
            exit 1
        }
    } else {
        Write-Host "Redis service is already running." -ForegroundColor Green
    }
    
    Write-Host "=========================================" -ForegroundColor Cyan
    Write-Host "All dependencies checked." -ForegroundColor Cyan
    Write-Host "=========================================" -ForegroundColor Cyan
}

# Check and start dependencies
Check-And-Start-Dependencies

Write-Host "=========================================" -ForegroundColor Cyan
Write-Host "    Starting Bilibili FastAPI Service" -ForegroundColor Cyan
Write-Host "=========================================" -ForegroundColor Cyan

# Set working directory
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path

# Check if network exists
Write-Host "Checking network configuration..." -ForegroundColor Yellow
try {
    $networkExists = docker network ls | Select-String -Pattern "bilibili-network"
    if (-not $networkExists) {
        Write-Host "Creating network server-network..." -ForegroundColor Yellow
        docker network create server-network
    } else {
        Write-Host "Network server-network already exists" -ForegroundColor Green
    }
} catch {
    Write-Host "Network check failed: $_" -ForegroundColor Red
    exit 1
}

# Start service
Write-Host "Starting FastAPI service..." -ForegroundColor Yellow
try {
    Set-Location -Path "$scriptDir\server"
    docker-compose -f 8-docker-compose-fastapi.yml up -d --build
    
    # Wait for service to start
    Write-Host "Waiting for service to start..." -ForegroundColor Yellow
    Start-Sleep -Seconds 5
    
    # Check container status
    Write-Host "Checking container status..." -ForegroundColor Yellow
    $containerStatus = docker ps | Select-String -Pattern "fastapi-server"
    
    if ($containerStatus) {
        Write-Host "=========================================" -ForegroundColor Green
        Write-Host "Service started successfully!" -ForegroundColor Green
        Write-Host "=========================================" -ForegroundColor Green
        Write-Host "Access address: http://localhost:8000" -ForegroundColor White
        Write-Host "API documentation: http://localhost:8000/docs" -ForegroundColor White
        Write-Host "Health check: http://localhost:8000/health" -ForegroundColor White
        Write-Host "=========================================" -ForegroundColor Green
        Write-Host "Common commands:" -ForegroundColor Yellow
        Write-Host "  View logs: docker logs -f fastapi-server" -ForegroundColor White
        Write-Host "  Stop service: docker-compose -f 8-docker-compose-fastapi.yml down" -ForegroundColor White
        Write-Host "  Restart service: docker-compose -f 8-docker-compose-fastapi.yml restart" -ForegroundColor White
        Write-Host "  Enter container: docker exec -it fastapi-server bash" -ForegroundColor White
        Write-Host "=========================================" -ForegroundColor Green
    } else {
        Write-Host "Service start failed. Please check logs for details" -ForegroundColor Red
        Write-Host "View logs command: docker logs fastapi-server" -ForegroundColor Yellow
    }
} catch {
    Write-Host "Service start failed: $_" -ForegroundColor Red
    # 恢复原始工作目录后退出
    Set-Location -Path $originalDir
    exit 1
}

# 恢复原始工作目录
Set-Location -Path $originalDir
